package rsj.service.customer.dynamic;

import org.apache.commons.lang3.StringUtils;
import rsj.api.customer.entity.Customer;

/**
 * 动态语句
 *
 * @author yuanqinglong
 * @since 2021/7/20 11:16
 */
public class CustomerDynamicStatement {


    /**
     * 客户列表动态sql
     *
     * @param custSname cust uname
     * @param custId    cust id
     * @param pageSize  页面大小
     * @param pageNow   页面现在
     * @return {@link String}
     */
    public String customerListDynamicSql(String custSname, String custId, Integer pageSize, Integer pageNow) {
        String sql = "SELECT CONCAT('RSJ',LPAD(cust_id::text,4,'0')) AS cust_id,\n" +
                "       cust_sname,\n" +
                "       cust_manbus,\n" +
                "       update_tm" +
                " FROM sys.sys_cust WHERE cust_id IS NOT NULL";
        sql += this.fuzzyQueryConditions(custSname, custId) + " ORDER BY update_tm DESC OFFSET #{pageNow} LIMIT #{pageSize}";
        return sql;
    }


    /**
     * 选择客户列表动态sql
     *
     * @param custSname cust uname
     * @param custId    cust id
     * @param pageSize  页面大小
     * @param pageNow   页面现在
     * @return {@link String}
     */
    public String customerSelectListDynamicSql(String custSname, String custId, Integer pageSize, Integer pageNow) {
        String sql = "SELECT CONCAT('RSJ',LPAD(cust_id::text,4,'0')) AS cust_show_id,cust_id,cust_sname,cust_manbus " +
                "FROM sys.sys_cust WHERE cust_id IS NOT NULL";
        sql += this.fuzzyQueryConditions(custSname, custId);
        sql += " ORDER BY CONVERT_TO(cust_sname,'GBK')";
        return sql;
    }


    /**
     * 客户列表动态sql
     * 客户列表统计动态sql
     *
     * @param custSname cust uname
     * @param custId    cust id
     * @param pageSize  页面大小
     * @param pageNow   页面现在
     * @return {@link String}
     */
    public String customerListDynamicCountSql(String custSname, String custId, Integer pageSize, Integer pageNow) {
        String sql = "SELECT COUNT(*) FROM sys.sys_cust WHERE cust_id IS NOT NULL";
        return sql + this.fuzzyQueryConditions(custSname, custId);
    }


    /**
     * 模糊查询条件
     *
     * @param custSname cust sname
     * @param custId    cust id
     * @return {@link String}
     */
    public String fuzzyQueryConditions(String custSname, String custId) {
        String sql = "";
        if (StringUtils.isNotBlank(custSname)) {
            sql += " AND cust_sname LIKE #{%custSname%}";
        }
        if (StringUtils.isNotBlank(custId)) {
            sql += " AND CONCAT('RSJ',LPAD(cust_id::text,4,'0')) LIKE #{%custId%}";
        }
        return sql;
    }

    /**
     * 动态更新客户
     *
     * @param customer 客户
     * @return {@link String}
     */
    public String dynamicUpdateCustomer(Customer customer) {
        String sql = "UPDATE sys.sys_cust SET update_tm = #{updateTm}";
        if (StringUtils.isNotBlank(customer.getCustAddr())) {
            sql += " ,cust_addr = #{custAddr}";
        }
        if (StringUtils.isNotBlank(customer.getCustManbus())) {
            sql += " ,cust_manbus = #{custManbus}";
        }
        if (StringUtils.isNotBlank(customer.getCustManbusmail())) {
            sql += " ,cust_manbusmail = #{custManbusmail}";
        }
        if (StringUtils.isNotBlank(customer.getCustManoper())) {
            sql += " ,cust_manoper = #{custManoper}";
        }
        if (StringUtils.isNotBlank(customer.getCustManopermail())) {
            sql += " ,cust_manopermail = #{custManopermail}";
        }
        if (StringUtils.isNotBlank(customer.getCustManoperphone())) {
            sql += " ,cust_manoperphone = #{custManoperphone}";
        }
        if (StringUtils.isNotBlank(customer.getCustManbusphone())) {
            sql += " ,cust_manbusphone = #{custManbusphone}";
        }
        if (StringUtils.isNotBlank(customer.getCustMantech())) {
            sql += " ,cust_mantech = #{custMantech}";
        }
        if (StringUtils.isNotBlank(customer.getCustMantechphone())) {
            sql += " ,cust_mantechphone = #{custMantechphone}";
        }
        if (StringUtils.isNotBlank(customer.getCustMantechmail())) {
            sql += " ,cust_mantechmail = #{custMantechmail}";
        }
        if (StringUtils.isNotBlank(customer.getCustLegname())) {
            sql += " ,cust_legname = #{custLegname}";
        }
        if (StringUtils.isNotBlank(customer.getCustSname())) {
            sql += " ,cust_sname = #{custSname}";
        }
        sql += " WHERE CONCAT('RSJ',LPAD(cust_id::text,4,'0')) LIKE #{%custId%}";
        return sql;
    }


}
